package com.lvcoding.auth2.auth.shizhan.controller;

import com.lvcoding.auth2.auth.shizhan.verifycode.parent.VerifyCode;
import com.lvcoding.auth2.auth.shizhan.verifycode.parent.VerifyCodeHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author wuyanshen
 */
@RequestMapping("code")
@RequiredArgsConstructor
@RestController
public class ValidateCodeController {

    private final Map<String, VerifyCodeHandler> verifyCodeHandlerMap;
    private final RedisTemplate redisTemplate;

    @GetMapping("{type}")
    public void sms(HttpServletRequest request, HttpServletResponse response, @PathVariable("type") String type) throws Exception {
        verifyCodeHandlerMap.get(type + "VerifyCodeHandler").handle(new ServletWebRequest(request, response));
    }

    @GetMapping("redis")
    public void getRedisSms() {
        VerifyCode verifyCode = (VerifyCode) redisTemplate.opsForValue().get("verify:code:sms:18911111111");
        boolean expired = verifyCode.isExpired();
        System.out.println("expired = " + expired);
        System.out.println("verifyCode = " + verifyCode);
    }
}
